<template>
    <section>
        <!--工具条-->

        <div class="section-content clearfix">
            <el-col :span="24" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">
                    <el-form-item>
                        <el-input v-model="filters.name" placeholder="用户名" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <!-- <el-form-item>
                        <el-select v-model="filters.type" placeholder="反馈类型" @change="getUsers()">
                            <el-option label="全部">
                            </el-option>
                            <el-option key="1" label="咨询" value="1">
                            </el-option>
                            <el-option key="2" label="建议" value="2">
                            </el-option>
                            <el-option key="3" label="BUG" value="3">
                            </el-option>
                            <el-option key="4" label="其他" value="4">
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item>
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>

                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="query" highlight-current-row v-loading="listLoading" @selection-change="selsChange" :header-row-style="{backgroundColor: titleColor}" style="width: 100%;">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column align="center" type="index" label="序号" width="80" :index="indexMethod">
                </el-table-column>
                <el-table-column align="center" prop="name" label="姓名">


                </el-table-column>
                <el-table-column align="center" prop="contactType" label="联系方式" :formatter="formatstate">
                </el-table-column>
                <el-table-column align="center" prop="contactValue" label="号码">
                </el-table-column>
                <el-table-column align="center" prop="type" label="类型" :formatter="formattype">
                </el-table-column>

                <el-table-column align="center" prop="status" label="状态" :formatter="formatstatus">

                </el-table-column>
                <el-table-column align="center" prop="createTime" label="时间">

                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                        <i @click="handleEdit(scope.$index, scope.row)" title="查看详情" icon="el-icon-edit" circle class="pb-icon icon-edit"></i>
                        <i @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete" circle title="删除" class="pb-icon icon-delete"></i>

                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                    style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="内容" v-show="editFormVisible" v-model="editFormVisible" :custom-class="dialogTitleColor" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm" style="min-height:200px">
                        <el-form-item label="故障报修">
        
                            <p>网关SN: {{content.sn}}</p>
                            <p>地点: {{content.address}}</p>
                            <!-- <p>机型: {{content.model}}</p> -->
                            <p>设备名称: {{content.model}}</p>
                            <p>问题描述: {{content.content}}</p>
                            <p>图片: <br/> <img :src="content.path" width="240"></p>
                            </el-form-item>
                            
                            <el-form-item label="回复内容">
                                <el-input type="textarea" v-model="editForm.content" ></el-input>
        
                            </el-form-item>
                     
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:right;">
                        <el-button @click.native="editFormVisible = false">取消</el-button>
                        <el-button :type="buttonColor" @click.native="editSubmit" :loading="editLoading">提交</el-button>
                    </div>
            </div>
        </el-dialog>

        <!--新增界面-->

    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        suggest_query,
        suggest_readed,
        suggest_get,
        suggest_delete,
        suggest_put,
        suggest_reply
    } from '../../api/api';

    export default {
        data() {
            return {
                content:'',
                disableditem: false,
                fileitem: [],
                fileList: {
                    softCode: '',
                    version: '',
                    name: '',
                    dir: '',
                    token: JSON.parse(localStorage.getItem('key'))
                },

                filters: {
                    name: '',
                    type: ''
                },
                file: '',
                query: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列
                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                
                },

                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],

                },
                //新增界面数据
                addForm: {
                    name: '',
                    coded: '',
                    version: '',
                    fileSize: '',
                    type: '1',
                    state: '1',
                    serverType: '',
                    remark: '',
                    ftpPath: '',
                    Path: ''
                }

            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            formatstatus(row, column) {

                return row.status == 3 ? '已回复' :row.status == 2 ? '已读' : row.status == 1 ? '未读' : '未知';
            },
            formatstate(row, column) {

                return row.contactType == 1 ? '电话' : row.contactType == 2 ? 'QQ' : row.contactType == 3 ? '微信' : row.contactType ==
                    4 ? '邮箱' : '未知';
            },
            formattype(row, column) {

                return row.type == 1 ? '咨询 ' : row.type == 2 ? '建议' : row.type == 3 ? 'BUG' : row.type == 4 ? '其他' :
                    '其他';
            },
            serverscade() {
                if (this.addForm.serverType == 0) {
                    this.fileList.dir = "http/SoftWave"
                } else {

                    this.fileList.dir = "ftp/SoftWave"
                }
            },
            details: function (index, row) {
                sessionStorage.setItem('communicate_details', row.id)
                this.$router.push({
                    path: '/devices/communicate/details'
                })
            },
            stateput(s) {
                if (s.state == 1) {
                    s.state = 2;
                } else {
                    s.state = 1;
                }
                let para = Object.assign({}, s);

                device_upgrade_put(para).then((res) => {

                     this.begCode(res.code,res.message);
                    if (res.code == 0) {
                        this.editLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '状态修改成功',
                            type: 'success'
                        });
                        this.$refs['editForm'].resetFields();
                        this.editFormVisible = false;
                        this.getUsers();
                    } else {
                        this.$message({
                            message: res.message,
                            type: 'warning'
                        });
                    }
                });
            },
            //性别显示转换
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件`);
            },
            handleAvatarSuccess(res, file) {
                console.log(file.type);

                this.begCode(res.code, res.message);
                if (res.code != 0) {
                    this.fileitem = [];
                }
                this.addForm.path = res.data.path;
                this.addForm.ftpPath = res.data.ftpPath;
                this.disableditem = true;
            },

            handleChange(file, fileList) {},
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取菜单列表
            getUsers() {
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    name: this.filters.name,
                    type: 5,
                };
                this.listLoading = true;
                //NProgress.start();
                suggest_query(para).then((res) => {

                     this.begCode(res.code,res.message);
                    this.total = res.data.totalCount;
                    this.query = res.data.list;
                    this.listLoading = false;
                    this.page=res.data.pageNo;
                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        idList: [row.id]
                    };
                    suggest_delete(para).then((res) => {

                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        } else {
                            this.$message({
                                message: res.message,
                                type: 'warning'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                let para = {
                    id: row.id
                }
                this.editForm={
                    id: row.id,
                    content:row.replyContent
                }
            
                this.content =JSON.parse(row.content);
                if(row.status!=3){
                    suggest_readed(para).then((res) => {

 this.begCode(res.code,res.message);
if (res.code == 0) {

    this.getUsers();
} 
});
                }
     
                console.log(this.content)
            },
            //显示新增界面
            handleAdd: function () {

                this.disableditem = false;
                this.addFormVisible = true;
                this.fileitem = [];
                this.fileList = {
                    softCode: '',
                    version: '',
                    name: '',
                    dir: 'http/SoftWave',
                    token: JSON.parse(localStorage.getItem('key'))
                };
                this.addForm = {
                    name: '',
                    coded: '',
                    version: '',
                    fileSize: '',
                    type: '1',
                    state: '1',
                    serverType: '0',
                    remark: '',
                    ftpPath: '',
                    path: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);

                            suggest_reply(para).then((res) => {

                                 this.begCode(res.code,res.message);
                                if (res.code == 0) {
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '回复成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: 'warning'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            console.log(this.addForm)
                            if (!this.addForm.path) {
                                this.$message({
                                    message: '请先上传文件',
                                    type: 'warning'
                                });
                                return false
                            }
                            this.addLoading = true;
                            //NProgress.start();
                            this.addForm.coded = this.fileList.softCode;
                            this.addForm.version = this.fileList.version;
                            let para = Object.assign({}, this.addForm);

                            device_upgrade_post(para).then((res) => {

                                 this.begCode(res.code,res.message);
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        idList: ids.split(',')
                    };
                    suggest_delete(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getUsers();
        }
    }
</script>

<style scoped>

</style>